﻿@{
    ViewBag.Title = "Danh sách chai";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('ProductController', function ($scope, $compile, $http, $filter, service) {
        $scope.loadCategory = function () {
            var url = '@url' + '@Url.Action("CategoryApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    $scope.categoriescmb.length=0;
                    $scope.categoriescmb.push({ "name": '-All-', "value": '' });
                    for (var i = 0; i < data.length; i++) {
                        $scope.categoriescmb.push({ "name": data[i].CategoryName, "value": data[i].CategoryID });
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.loadPartner = function () {
            var url = '@url' + '@Url.Action("PartnerApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    $scope.partnerscmb.length=0;
                    $scope.ownerscmb.length=0;
                    $scope.partners.length=0;
                    $scope.partnerscmb.push({ "name": '-All-', "value": '' });                    
                    $scope.ownerscmb.push({ "name": '-All-', "value": '' });
                    for (var i = 0; i < data.length; i++) {
                        $scope.partnerscmb.push({ "name": data[i].PartnerName, "value": data[i].PartnerID });
                        $scope.ownerscmb.push({ "name": data[i].PartnerName, "value": data[i].PartnerID });
                        $scope.partners[data[i].PartnerID]=data[i].PartnerName;
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.loadLot = function () {
            var url = '@url' + '@Url.Action("LotApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    $scope.lotscmb.length=0;
                    $scope.lotscmb.push({ "name": '-All-', "value": '' });
                    for (var i = 0; i < data.length; i++) {
                        $scope.lotscmb.push({ "name": data[i].LotCode, "value": data[i].LotID });
                    }
                    
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.load = function (page) {
            if (page === 0 || (page > $scope.pageCount && $scope.pageCount > 0))
                return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            if($scope.firstload)
            {
                var model = @Html.Raw(Json.Encode(Request.Url.AbsolutePath.Split('/')[Request.Url.AbsolutePath.Split('/').Length - 1]));
                if(isNaN(parseInt(model,10)))  $scope.lotID = { name: '', value: '' };
                else  $scope.lotID = { name: '', value: parseInt(model) };
                $scope.firstload = false;
            }

            startloading($('#content'));
            var url = '@url' + '@Url.Action("ProductApi", "Api")';
            $http.get(url, {
                params: {
                    pageNumber: page,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    productCode: $scope.productCode,
                    categoryID: $scope.categoryID.value,
                    ownerID: $scope.ownerID.value,
                    partnerID: $scope.partnerID.value,
                    lotID: $scope.lotID.value,
                    status: $scope.status.value,
                    expiredfrom: $filter('date')(Date.parse($scope.expiredfrom), 'y/MM/dd'),
                    expiredto: $filter('date')(Date.parse($scope.expiredto), 'y/MM/dd')
                }
            })
                .success(function (data) {
                    service.setCommonData($scope, data, data.Products);
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data.InnerException.InnerException);
                });
        };

        $scope.setSelected = function (id) {
            service.setSelected($scope, id);
        };

        $scope.check = function (id) {
            $scope.checkedObj[id] = !$scope.checkedObj[id];
        };

        $scope.setSelectedAll = function () {
            service.setSelectedAll($scope);
        };

        $scope.refresh = function (keyCode) {
            if (keyCode == undefined || keyCode === 13)
                $scope.load($scope.pageNumber);
        };

        $scope.clearrefresh = function () {
            $scope.productCode = '';
            $scope.categoryID = { name: '', value: '' };
            $scope.partnerID = { name: '', value: '' };
            $scope.ownerID = { name: '', value: '' };
            $scope.lotID = { name: '', value: '' };
            $scope.status = { name: '', value: '' };
            $scope.expiredfrom = '';
            $scope.expiredto = '';
            $scope.load($scope.pageNumber);
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "Product")';
        };

        $scope.edit = function (id) {
            if (id) {
                window.location = '@Url.Action("Detail", "Product")/' + id;
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.location = '@Url.Action("Detail", "Product")/' + selectedObj;
            }
        };

        $scope.createtest = function (id) {
            if (id) {
                window.open('@Url.Action("Detail", "TestProduct")/P' + id);
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.open('@Url.Action("Detail", "TestProduct")/P' + selectedObj);
            }
        };

        $scope.viewtest = function (id) {
            if (id) {
                window.location = '@Url.Action("Index", "TestProduct")/Index/' + id;
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.location = '@Url.Action("Index", "TestProduct")/Index/' + selectedObj;
            }
        };

        $scope.expired = function (page) {
            if (page === 0 || (page > $scope.pageCount && $scope.pageCount > 0))
                return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];

            startloading($('#content'));
            var url = '@url' + '@Url.Action("ProductApi", "Api")';
            $http.get(url, {
                params: {
                    pageNumber: page,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    productCode: $scope.productCode
                }
            })
                .success(function (data) {
                    service.setCommonData($scope, data, data.Products);
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data.InnerException.InnerException);
                });
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var deleteObj = service.getSelected($scope);
            if (deleteObj === '') return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("ProductApi", "Api")';
            $http.delete(url, {
                params: {
                    ids: deleteObj,
                    pageNumber: $scope.pageNumber,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    productCode: $scope.productCode,
                    categoryID: $scope.categoryID.value,
                    ownerID: $scope.ownerID.value,
                    partnerID: $scope.partnerID.value,
                    lotID: $scope.lotID.value,
                    status: $scope.status.value,
                    expiredfrom: $filter('date')(Date.parse($scope.expiredfrom), 'y/MM/dd'),
                    expiredto: $filter('date')(Date.parse($scope.expiredto), 'y/MM/dd')
                }
            }).success(function (data) {
                service.setCommonData($scope, data, data.Products);
                stoploading($('#content'));
                service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được xóa thành công!!!');
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.sort = function (ob) {
            service.setSortingValue($scope, ob);
            $scope.load($scope.pageNumber);
        };

        $scope.categoriescmb = [{}];
        $scope.partnerscmb = [{}];
        $scope.ownerscmb = [{}];
        $scope.lotscmb = [{}];

        $scope.statuscmb = [
            { name: '-All-', value: '' },
                { name: 'Đạt', value: 'D' },
                { name: 'Không đạt', value: 'K' },
                { name: 'Mất', value: 'M' },
        ];

        $scope.statuss = [{}];
        $scope.statuss['D'] = 'Đạt';
        $scope.statuss['K'] = 'Không đạt';
        $scope.statuss['M'] = 'Mất';

        $scope.sizes = [{}];
        $scope.sizes['L'] = 'Lớn';
        $scope.sizes['N'] = 'Nhỏ';

        $scope.partners = [{}];
 
        $scope.firstload = true;
        $scope.orderBy = 'productID';
        $scope.orderDirection = 'descending';
        $scope.productCode = '';
        $scope.categoryID = {name:'',value:''};
        $scope.partnerID={name:'',value:''};
        $scope.ownerID={name:'',value:''};
        $scope.lotID={name:'',value:''};
        $scope.status={name:'',value:''};
        $scope.checkedList = [];
        $scope.checkedObj = [{}];
        $scope.loadCategory();
        $scope.loadPartner();
        $scope.loadLot();
        $scope.load(1);
    });


</script>
<div ng-controller="ProductController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_ListViewHeader")
    <div id="content" class="content row">
        <form role="form" name="form" autocomplete="off">
            <div class="panel-body">
                <div id="search">
                    <div class="row" style="margin-right: 0px">
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_TextBox", new Dictionary<string, string>
                            {
                                {"Text", "Mã số chai"},
                                {"Model", "productCode"},
                                {"Name", "productCode"},
                                {"MaxLength","12"},
                                {"KeyPress","refresh($event.keyCode)"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Loại chai"},
                                {"Model", "categoryID"},
                                {"Name", "categoryID"},
                                {"Options", "categoryID.name for categoryID in categoriescmb track by categoryID.value"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Lô"},
                                {"Model", "lotID"},
                                {"Name", "lotID"},
                                {"Options", "lotID.name for lotID in lotscmb track by lotID.value"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Tình trạng"},
                                {"Model", "status"},
                                {"Name", "status"},
                                {"Options", "status.name for status in statuscmb track by status.value"}
                            })
                        </div>
                        <div class="col-lg-2" style="clear: both">
                            @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Chủ sở hữu"},
                                {"Model", "ownerID"},
                                {"Name", "ownerID"},
                                {"Options", "ownerID.name for ownerID in ownerscmb track by ownerID.value"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Vị trí"},
                                {"Model", "partnerID"},
                                {"Name", "partnerID"},
                                {"Options", "partnerID.name for partnerID in partnerscmb track by partnerID.value"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_DatePicker", new Dictionary<string, string>
                            {
                                {"Text", "Niên hạn từ"},
                                {"Model", "expiredfrom"},
                                {"Name", "expiredfrom"}
                            })
                        </div>
                        <div class="col-lg-2">
                            @Html.Partial("Control/_Control_DatePicker", new Dictionary<string, string>
                            {
                                {"Text", "đến"},
                                {"Model", "expiredto"},
                                {"Name", "expiredto"}
                            })
                        </div>
                        @Html.Partial("_Search")
                    </div>
                </div>
                @Html.Partial("_Toolbar_PagedList")
                <div class="table-responsive">
                    <table id="data" class="table table-striped table-bordered table-hover">
                        <colgroup>
                            <col style="width: 50px" />
                            <col />
                            <col style="width: 80px" />
                            <col style="width: 150px" />
                            <col style="width: 150px" />
                            <col style="width: 60px" />
                            <col style="width: 60px" />
                            <col style="width: 50px" />
                            <col style="width: 50px" />
                            <col style="width: 100px" />
                            <col style="width: 100px" />
                            <col style="width: 70px" />
                        </colgroup>
                        <thead>
                            <tr>
                                <th>
                                    <div>
                                        <input ng-model="selectedAll" ng-click="setSelectedAll()" style="margin-left: 10px;" class="center" type="checkbox">
                                    </div>
                                </th>
                                <th ng-click="sort('Barcode')">Barcode
                                <div ng-class="orderBy != 'Barcode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('ProductCode')">MSChai
                                <div ng-class="orderBy != 'ProductCode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('OwnerID')">Chủ sở hữu
                                <div ng-class="orderBy != 'OwnerID' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('PartnerID')">Vị trí
                                <div ng-class="orderBy != 'PartnerID' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('CategoryName')">Loại
                                <div ng-class="orderBy != 'CategoryName' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('Size')">KT
                                <div ng-class="orderBy != 'Size' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('CurrentWeight')">TLTT
                                <div ng-class="orderBy != 'CurrentWeight' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('CurrentVolume')">DTTT
                                <div ng-class="orderBy != 'CurrentVolume' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('Expired')">Niên Hạn
                                <div ng-class="orderBy != 'Expired' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('TestedDate')">Ngày KN
                                <div ng-class="orderBy != 'TestedDate' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                                <th ng-click="sort('Status')">TT
                                <div ng-class="orderBy != 'Status' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-hide="totalItemOnPage == 0" ng-repeat="item in items" ng-click="setSelected(item.ProductID)"
                                ng-dblclick="edit(item.ProductID)">
                                <td class="center">
                                    <div>
                                        <input ng-click="check(item.ProductID)" ng-model="checkedObj[item.ProductID]" class="center" type="checkbox" />
                                    </div>
                                </td>
                                <td>
                                    @{
                                        var detailurl = Url.Action("Detail", "Product", new { id = "{{id=item.ProductID}}" });
                                        detailurl = HttpUtility.UrlDecode(detailurl);
                                    }
                                    <a data-ng-href="@detailurl">{{item.Barcode}}</a>
                                </td>
                                <td>{{item.ProductCode}}</td>
                                <td>{{partners[item.OwnerID]}}</td>
                                <td>{{partners[item.PartnerID]}}</td>
                                <td>{{item.CategoryName}}</td>
                                <td>{{sizes[item.Size]}}</td>
                                <td>{{item.TestWeight}}</td>
                                <td>{{item.TestVolume}}</td>
                                <td class="center">{{item.Expired | date:'dd/MM/y'}}</td>
                                <td class="center">{{item.TestedDate | date:'dd/MM/y'}}</td>
                                <td>{{statuss[item.Status]}}</td>
                            </tr>
                            <tr ng-show="totalItemOnPage == 0">
                                <td colspan="100">Không có dữ liệu
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </form>
    </div>
